﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using UMS.DBUtility;
//                 o8888888o  
//                 88" . "88  
//                 (| -_- |)  
//                 O\  =  /O  
//              ____/`---'\____  
//            .'  \\|     |//  `.  
//          /  \\|||  :  |||//  \  
//          /  _||||| -:- |||||-  \  
//          |   | \\\  -  /// |   |  
//          | \_|  ''\---/''  |   |  
//          \  .-\__  `-`  ___/-. /  
//        ___`. .'  /--.--\  `. . __  
//     ."" '<  `.___\_<|>_/___.'  >'"".  
//    | | :  `- \`.;`\ _ /`;.`/ - ` : | |  
//    \  \ `-.   \_ __\ /__ _/   .-` /  /  
//=====`-.____`-.___\_____/___.-`____.-'======  
//                  `=---='  
//  
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
//          佛祖保佑       永无Bug  
//          快加工资       不改需求 
namespace DataCollectionDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public Socket newclient;
        public bool Connected;
        public delegate void MyInvoke(string str);
        private static DispatcherTimer timersend = new DispatcherTimer();
        public string stringdata;
        public Thread mThread;
        public ThreadStart myThreadStart;
        public Thread mThread1;
        public ThreadStart myThreadStart1;
        delegate void SetTextCallback(string text, int i);
        delegate void SetTextCallback1(Byte[] text);
        public int number;
        public MainWindow()
        {
            InitializeComponent();
        }

        public void Connect()
        {

            byte[] data = new byte[1024];
            //获取输入框服务地址和端口号
            String IP = serverIP.Text.Trim();
            int Port = Convert.ToInt32(port.Text.Trim());
            //创建连接套词
            IPEndPoint ie = new IPEndPoint(IPAddress.Parse("192.168.51.199"), 502);
            newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //尝试连接，否则抛出异常
            try
            {
                //进行服务器连接
                newclient.Connect(ie);

                connect.IsEnabled = false;

                Connected = true;


            }
            catch (SocketException e)
            {
                MessageBox.Show("连接服务器失败" + e.Message);
                return;
            }
       
            //myThreadStart = new ThreadStart(reciveData);
            //mThread = new Thread(myThreadStart);
            //mThread.IsBackground = true;
            //mThread.Start();
          
            timersend.Tick += new EventHandler(sendDataTest);
            //timersend.Tick += new EventHandler(SendLongData1);
            //timersend.Tick += new EventHandler(SendLongData2);
            //timersend.Tick += new EventHandler(SendLongData3);
            //timersend.Tick += new EventHandler(SendLongData4);
            //timersend.Tick += new EventHandler(SendLongData5);
            //timersend.Tick += new EventHandler(SendLongData6);
            //timersend.Tick += new EventHandler(SendLongData7);
            //timersend.Tick += new EventHandler(SendLongData8);
            timersend.Interval = new TimeSpan(0, 0, 0, 10);


            timersend.Start();
      




        }
        //采集风扇马达启动开关
        private void SendFanMotorStartMsg(object sender,EventArgs e)
        {
            //byte[] data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x09, 0xf6, 0x00, 0x01 };
            //newclient.Send(data);
            //myThreadStart = new ThreadStart(ReceiveFanMotorStarMsg);
            //mThread = new Thread(myThreadStart);
            //mThread.Start(); mThread.DisableComObjectEagerCleanup();
            //while (true)
            //{
            //    for (int i = 0; i < 40; i++)
            //    {
            //        sendData(i);
            //    }

            //}
           
                for (int i = 0; i < 40; i++)
                {
                    sendData(i);
                  //  ReceiveFanMotorStarMsg();
                }
            
        }
     

        //接收参数
        private void ReceiveFanMotorStarMsg()
        {
            //while (true)
            //{
            byte[] data = new byte[1024];//定义数据接收数组  
            newclient.Receive(data);//接收数据到data数组  

            int length = data[5];//读取数据长度  
            Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
            for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                datashow[i] = data[i];
            stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串 

            switch (data[1])
            {
                case 0x01:
                    number = 0;
                    stringdata = isOn(data);
                    break;
                case 0x02:
                    number = 1;
                    stringdata = isOn(data);
                    break;
                case 0x03:
                    number = 2;
                    stringdata = isOn(data);
                    break;
                case 0x04:
                    number = 3;
                    stringdata = isOn(data);
                    break;
                case 0x05:
                    number = 4;
                    stringdata = isOn(data);
                    break;
                case 0x06:
                    number = 5;
                    stringdata = isOn(data);
                    break;
                case 0x07:
                    number = 6;
                    if (isTrue(data))
                        stringdata = byteToString(data[11], data[12]);
                    break;
                case 0x08:
                    number = 7;
                    if (isTrue(data))
                        stringdata = byteToString(data[11], data[12]);
                    break;
                case 0x09:
                    number = 8;
                    if (isTrue(data))
                        stringdata = byteToString(data[11], data[12]);
                    break;
                case 0x0A:
                    number = 9;
                    if (isTrue(data))
                        stringdata = switchState(data[12]);
                    break;
                case 0x0B:
                    number = 10;
                    if (isTrue(data))
                        stringdata = switchState(data[12]);
                    break;
                case 0x0C:
                    number = 11;
                    if (isTrue(data))
                    {
                        switch (data[12])
                        {
                            case 0x00:
                                stringdata = "选定值1";
                                break;
                            case 0x01:
                                stringdata = "选定值2";
                                break;
                            default:
                                stringdata = "未知状态";
                                break;
                        }
                    }
                    break;
                case 0x0D:
                    number = 12;
                    stringdata = ThirdData(data);
                    break;
                case 0x0E:
                    number = 13;
                    stringdata = ThirdData(data);
                    break;
                case 0x0F:
                    number = 14;
                    stringdata = ThirdData(data);
                    break;
                case 0x10:
                    number = 15;
                    stringdata = ThirdData(data);
                    break;
                case 0x11:
                    number = 16;
                    stringdata = ThirdData(data);
                    break;
                case 0x12:
                    number = 17;
                    stringdata = ThirdData(data);
                    break;
                case 0x13:
                    number = 18;
                    if (data[5] == 0x05 && data[6] == 0x01 && data[7] == 0x03)
                    {
                        if (data[10] == 0x01)
                            stringdata = "设定点1";
                        else
                            stringdata = "设定点2";
                    }
                    else
                    {
                        stringdata= "未知的参数";
                    }
                    break;
                case 0x14:
                    number = 19;
                    if (data[5] == 0x05 && data[6] == 0x01 && data[7] == 0x03)
                    {
                        if (data[10] == 0x00)
                            stringdata = "关";
                        else
                            stringdata = "开";
                    }
                    else
                    {
                        stringdata = "未知的参数";
                    }
                    break;
                case 0x15:
                    number = 20;
                    if (data[5] == 0x07 && data[6] == 0x01 && data[7] == 0x03)
                    {
                        byte[] a = { data[9], data[10] };
                        byte[] b = { data[11], data[12] };
                        short x = (short)((a[0] << 8) + a[1]);
                        short y = (short)((b[0] << 8) + b[1]);
                        stringdata = ((float)x/y).ToString();
                    }
                    else
                    {
                        stringdata ="参数错误";
                    }
                    break;
                case 0x16:
                    number = 21;
                    stringdata = FourthData(data);
                    break;
                case 0x17:
                    number = 22;
                    stringdata = FourthData(data);
                    break;
                case 0x18:
                    number = 23;
                    stringdata = FourthData(data);
                    break;
                case 0x19:
                    number = 24;
                    stringdata = FourthData(data);
                    break;
                case 0x1a:
                    number = 25;
                    stringdata = FourthData(data);
                    break;
                case 0x1b:
                    number = 26;
                    stringdata = FourthData(data);
                    break;
                case 0x1c:
                    number = 27;
                    stringdata = FourthData(data);
                    break;
                case 0x1d:
                    number = 28;
                    stringdata = FourthData(data);
                    break;
                case 0x1e:
                    number = 29;
                    stringdata = FourthData(data);
                    break;
                case 0x1f:
                    number = 30;
                    stringdata = FourthData(data);
                    break;
                case 0x20:
                    number = 31;
                    stringdata = FourthData(data);
                    break;
                case 0x21:
                    number = 32;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x22:
                    number = 33;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x23:
                    number = 34;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x24:
                    number = 35;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x25:
                    number = 36;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x26:
                    number = 37;
                    stringdata = byteToString(data[9], data[10]);
                    break;
                case 0x27:
                    number = 38;
                    break;
                case 0x28:
                    number = 39;
                    break;


            }
            SetTextCallback d = new SetTextCallback(SetText);   // 托管调用  
            this.Dispatcher.Invoke(d, new object[] { stringdata, number });


            //  }

        }
        // 根据接收的参数进行赋值
        private void SetText(string text, int code)
        {
            switch (code)
            {
                //First Part 采集第一部分数据
                case 0:
                    //风扇马达启动
                    datacollect.Text = text;
                    break;
                case 1:
                    //排气开关
                    datacollect2.Text = text;
                    break;
                case 2:
                    // 采集普通停机开关
                    datacollect3.Text = text;
                    break;
                case 3:
                    // 采集自动运行开关
                    datacollect4.Text = text;
                    break;
                case 4:
                    // 采集普通警告开关
                    datacollect5.Text = text;
                    break;
                case 5:
                    // 采集允许启动运行主马达开关
                    datacollect6.Text = text;
                    break;

                //Second Part 采集第二部分数据 采集7个数据
                case 6:
                    //压缩机出口
                    datacollect7.Text = text;
                    break;
                case 7:
                    // 采集主机出口信息
                    datacollect8.Text = text;
                    break;
                case 8:
                    // 采集环境空气温度信息
                    datacollect9.Text = text;
                    break;
                case 9:
                    //采集紧急停机开关
                    datacollect10.Text = text;
                    break;
                case 10:
                    //采集风扇电机过载开关
                    datacollect11.Text = text;
                    break;
                //       case 11:
                //          //采集远程启动/停机开关(未用到)
                //           datacollect12.Text = text;
                //          break;
                case 11:
                    //压力设定选择
                    datacollect13.Text = text;
                    break;

                //Third Part 第三部分数据 采集9个数据

                case 12:
                    // 设定点1
                    datacollect14.Text = text;
                    break;
                case 13:
                    // 1级间接停机点
                    datacollect15.Text = text;
                    break;
                case 14:
                    //1级直接停机点
                    datacollect16.Text = text;
                    break;
                case 15:
                    //设定点 2
                    datacollect17.Text = text;
                    break;
                case 16:
                    //2级间接停机点
                    datacollect18.Text = text;
                    break;
                case 17:
                    //2级直接停机点
                    datacollect19.Text = text;
                    break;
                case 18:
                    // 使用的设定点
                    datacollect20.Text = text;
                    break;
                //case 20:
                //    // 调节模式
                //    datacollect21.Text = text;
                //    break;
                case 19:
                    //数字压力带选择
                    datacollect22.Text = text;
                    break;
                case 20:
                    //最高转速因数
                    datacollect23.Text = text;
                    break;

                //第四部分数据采集  采集11个数据
                case 21:
                    // 运行时间
                    datacollect24.Text = text;
                    break;
                case 22:
                    // 电机启动次数
                    datacollect25.Text = text;
                    break;
                case 23:
                    //加载继电器
                    datacollect26.Text = text;
                    break;
                case 24:
                    //变频驱动1-20%速度
                    datacollect27.Text = text;
                    break;
                case 25:
                    //变频驱动20-40%速度
                    datacollect28.Text = text;
                    break;
                case 26:
                    //变频驱动40-60%速度
                    datacollect29.Text = text;
                    break;
                case 27:
                    // 变频驱动60-80%速度
                    datacollect30.Text = text;
                    break;
                case 28:
                    //变频驱动80-100%速度
                    datacollect31.Text = text;
                    break;
                case 29:
                    //风扇启动
                    datacollect32.Text = text;
                    break;
                case 30:
                    //累计流量
                    datacollect33.Text = text;
                    break;
                case 31:
                    // 控制器运行时间
                    datacollect34.Text = text;
                    break;
                case 32:
                    // 冷干机出口温度
                    datacollect39.Text = text;
                    break;
                case 33:
                    // 冷干机露点温度
                    datacollect40.Text = text;
                    break;
                case 34:
                    // 冷干机出气温度
                    datacollect41.Text = text;
                    break;
                case 35:
                    // 冷干机冷凝温度
                    datacollect42.Text = text;
                    break;
                case 36:
                    // 冷干机蒸汽温度
                    datacollect43.Text = text;
                    break;
                case 37:
                    // 冷干机压力值
                    datacollect44.Text = text;
                    break;
                case 38:
                    // 冷干机故障类型
                    datacollect45.Text = text;
                    break;
                case 39:
                    // 冷干机运行状态
                    datacollect46.Text = text;
                    break;
                default:
                    break;
            }
        }
        //发送数据到机器中
        private void sendData(int code)
        {
            byte[] data = null;
            switch (code)
            {
                //First 采集数据5个
                case 0:
                    //风扇马达启动 地址2550
                    data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf6, 0x00, 0x01 };
                    break;
                case 1:
                    //排气开关 地址2553
                    data = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf9, 0x00, 0x01 };
                    break;
                case 2:
                    // 采集普通停机开关 地址2554
                    data = new byte[] { 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfa, 0x00, 0x01 };
                    break;
                case 3:
                    // 采集自动运行开关 地址2556
                    data = new byte[] { 0x00, 0x04, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfc, 0x00, 0x01 };
                    break;
                case 4:
                    // 采集普通警告开关 地址2557
                    data = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfd, 0x00, 0x01 };
                    break;
                case 5:
                    // 采集允许启动运行主马达开关 地址2558
                    data = new byte[] { 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xfe, 0x00, 0x01 };
                    break;

                //Second  采集数据7个
                case 6:
                    //压缩机出口  地址0
                    data = new byte[] { 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
                    break;
                case 7:
                    // 采集主机出口信息 地址2  测试发现地址为4
                    data = new byte[] { 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x04, 0x00, 0x02 };
                    break;
                case 8:
                    // 采集环境空气温度信息 地址4  测试发现地址为8
                    data = new byte[] { 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x08, 0x00, 0x02 };
                    break;
                case 9:
                    //采集紧急停机开关 地址T500
                    data = new byte[] { 0x00, 0x0A, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf4, 0x00, 0x02 };
                    break;
                case 10:
                    //采集风扇电机过载开关 地址501  测试为502
                    data = new byte[] { 0x00, 0x0B, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf6, 0x00, 0x02 };
                    break;
                //  case 11:
                //    //采集远程启动/停机开关(未用到) 地址502
                //    data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x01, 0x01, 0xf6, 0x00, 0x02 };
                //  break;
                case 11:
                    //采集压力设定选择开关 地址509
                    data = new byte[] { 0x00, 0x0C, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x02, 0x06, 0x00, 0x02 };
                    break;

                //Third Part第三部分 采集数据9个

                case 12:
                    // 设定点1 地址851
                    data = new byte[] { 0x00, 0x0D, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x53, 0x00, 0x01 };
                    break;
                case 13:
                    // 1级间接停机点 地址881  找不到停机点地址
                    data = new byte[] { 0x00, 0x0E, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x71, 0x00, 0x01 };
                    break;
                case 14:
                    //1级直接停机点 地址882
                    data = new byte[] { 0x00, 0x0F, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x72, 0x00, 0x01 };
                    break;
                case 15:
                    //设定点 2 地址852
                    data = new byte[] { 0x00, 0x10, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x54, 0x00, 0x01 };
                    break;
                case 16:
                    //2级间接停机点 地址883
                    data = new byte[] { 0x00, 0x11, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x73, 0x00, 0x01 };
                    break;
                case 17:
                    //2级直接停机点 地址884
                    data = new byte[] { 0x00, 0x12, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x74, 0x00, 0x01 };
                    break;
                case 18:
                    // 使用的设定点 地址850
                    data = new byte[] { 0x00, 0x13, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x52, 0x00, 0x01 };
                    break;
                //case 20:
                //    // 调节模式  发送数据待定
                //    data = new byte[] { 0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x04, 0x00, 0x01 };
                //    break;
                case 19:
                    //数字压力带选择 地址880
                    data = new byte[] { 0x00, 0x14, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x70, 0x00, 0x01 };
                    break;
                case 20:
                    //最高转速因数 发送数据待定 800 取到的两个个实际转速和预期转速的比为该因数
                    data = new byte[] { 0x00, 0x15, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x20, 0x00, 0x02 };
                    break;


                //第四部分 采集数据11个
                case 21:
                    // 运行时间 地址1000
                    data = new byte[] { 0x00, 0x16, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xe8, 0x00, 0x01 };
                    break;
                case 22:
                    // 电机启动次数 地址1002
                    data = new byte[] { 0x00, 0x17, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xed, 0x00, 0x01 };
                    break;
                case 23:
                    //加载继电器 地址1003
                    data = new byte[] { 0x00, 0x18, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xef, 0x00, 0x01 };
                    break;
                case 24:
                    //采集变频驱动1-20%速度数据 地址1004
                    data = new byte[] { 0x00, 0x19, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xec, 0x00, 0x01 };
                    break;
                case 25:
                    //采集变频驱动20%-40%速度数据 地址1005 测试为点击启动次数
                    data = new byte[] { 0x00, 0x1A, 0x00 ,0x00, 0x00,  0x06, 0x01, 0x03, 0x03, 0xed, 0x00, 0x01 };
                    break;
                case 26:
                    //采集变频驱动40%-60%速度数据 地址1006
                    data = new byte[] { 0x00, 0x1B, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xee, 0x00, 0x01 };
                    break;
                case 27:
                    // 采集变频驱动60%-80%速度数据 地址1007 测试为加载继电器数据
                    data = new byte[] { 0x00, 0x1C, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xef, 0x00, 0x01 };
                    break;
                case 28:
                    // 采集变频驱动80%-100%速度数据  地址1008
                    data = new byte[] { 0x00, 0x1D, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf0, 0x00, 0x01 };
                    break;
                case 29:
                    //风扇启动次数 地址1010
                    data = new byte[] { 0x00, 0x1E, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf2, 0x00, 0x01 };
                    break;
                case 30:
                    //采集累计流量数据 地址1011
                    data = new byte[] { 0x00, 0x1F, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf3, 0x00, 0x01 };
                    break;
                case 31:
                    //采集控制器运转时间数据 地址1012
                    data = new byte[] { 0x00, 0x20, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xf4, 0x00, 0x01 };
                    break;
                case 32:
                    //采集冷干机数据 地址40001
                    data = new byte[] { 0x00, 0x21, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x41, 0x00, 0x01 };
                    break;
                case 33:
                    //采集冷干机数据 地址40002
                    data = new byte[] { 0x00, 0x22, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x42, 0x00, 0x01 };
                    break;
                case 34:
                    //采集冷干机数据 地址40003
                    data = new byte[] { 0x00, 0x23, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x43, 0x00, 0x01 };
                    break;
                case 35:
                    //采集冷干机数据 地址40004
                    data = new byte[] { 0x00, 0x24, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x44, 0x00, 0x01 };
                    break;
                case 36:
                    //采集冷干机数据 地址40005
                    data = new byte[] { 0x00, 0x25, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x45, 0x00, 0x01 };
                    break;
                case 37:
                    //采集冷干机数据 地址40006
                    data = new byte[] { 0x00, 0x26, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x46, 0x00, 0x01 };
                    break;
                case 38:
                    //采集冷干机数据 地址40007
                    data = new byte[] { 0x00, 0x27, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x47, 0x00, 0x01 };
                    break;
                case 39:
                    //采集冷干机数据 地址40008
                    data = new byte[] { 0x00, 0x28, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x48, 0x00, 0x01 };
                    break;

                //第五部分 采集4个数据


                default:
                    MessageBox.Show("参数错误");
                    break;
            }
            newclient.Send(data);
         
          
        }
        //连接按钮事件
        private void connect_Click(object sender, RoutedEventArgs e)
        {
            //校验端口号以及服务地址
            if (initIP())
            {
                Connect();

            }
        }
        //检测IP地址是否正确
        private Boolean initIP()
        {
            Regex validipregex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
            if (serverIP.Text.Trim() != null && serverIP.Text.Trim().Length != 0 && serverIP.Text.Trim() != "" && validipregex.IsMatch(serverIP.Text.Trim()))
            {
                return true;
            }
            else
            {
                MessageBox.Show("服务器地址输入有误，请重新输入");
                return false;
            }
        }
        //第一部分开关数据判断
        private String isOn(Byte[] text)
        {
            string result = " ";
            if (text[5] == 0x05 && text[6] == 0x01 && text[7] == 0x03)
            {
                switch (text[10])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知";
                        break;
                }

            }
            else
            {
                result = "假的假的";
            }
            return result;
        }
        //判断接收数据是否正确
        private Boolean isTrue(Byte[] text)
        {
            if (text[6] == 0x01 && text[7] == 0x03 && text[10] == 0x80)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        //字节转换整形字符串
        private String byteToString(byte data1, byte data2)
        {
            byte[] b = { data1, data2 };
            short x = (short)((b[0] << 8) + b[1]);
            string stringResult = x.ToString();
            return stringResult;
        }
        //判断第二部分数据开关
        private String switchState(byte data)
        {
            switch (data)
            {
                case 0x00:
                    return "开";
                case 0x01:
                    return "关";
                default:
                    return "未知状态";

            }
        }
        //判断第三部分数据
        private String ThirdData(Byte[] data)
        {

            if (data[5] == 0x05 && data[6] == 0x01 && data[7] == 0x03)
            {
                return byteToString(data[9],data[10]);
            }
            else
            {
                return "未知的参数";
            }
        }
        //第四部分数据处理
        private String FourthData(Byte[] data)
        {
            if(data[5]==0x05 && data[6]==0x01 && data[7] == 0x03)
            {
                byte[] a = { data[9], data[10] };
                short x = (short)((a[0] << 8) + a[1]);
                stringdata = x.ToString();
            }
            else
            {
                stringdata = "参数错误";
            }
            return stringdata;
        }
        //发送数据
        private void SendLongData(object sender, EventArgs e)
        {

            byte[] data = new byte[] {  0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x52, 0x00, 0x06 };
            for (int number = 6; number < 7; number++)
            {
                switch (number)
                {
                    case 0:
                        //  风扇马达启动 地址2550
                        data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf6, 0x00, 0x10 };
                        break;
                    case 1:
                        //    使用的设定点 地址850
                        data = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x52, 0x00, 0x06 };
                        break;
                    case 2:
                        //  数字压力带选择 地址880 未采集到数据
                        data = new byte[] { 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x70, 0x00, 0x06 };
                        break;
                    case 3:
                        //  最高转速因数 发送数据待定 800 取到的两个个实际转速和预期转速的比为该因数
                        data = new byte[] { 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x20, 0x00, 0x02 };
                        break;
                    case 4:
                        //  运行时间 地址1000
                        data = new byte[] { 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xe8, 0x00, 0x20 };
                        break;
                    case 5:
                        //采集冷干机数据 地址40001
                        data = new byte[] { 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x41, 0x00, 0x08 };
                        break;
                    case 6:
                        data = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x10 };
                        break;
                    case 7:
                        data = new byte[] { 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf4, 0x00, 0x16 };
                        break;
                    case 8:
                        data = new byte[] { 0x00, 0x04, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x02, 0x06, 0x00, 0x02 };
                        break;

                }
                newclient.Send(data);
                reciveData();
            }
        }

        private void reciveData()
        {
            while (true)
            {
                byte[] data = new byte[1024];//定义数据接收数组  

                newclient.Receive(data);//接收数据到data数组  
                int length = data[5];//读取数据长度  
                Byte[] datashow = new byte[length + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow[i] = data[i];
                //SetTextCallback1 d = new SetTextCallback1(setText);   // 托管调用  
                //this.Dispatcher.Invoke(d, new object[] { datashow });
                if (datashow[1] == 0x01)
                {
                    string sqlstr = "server=192.168.4.124;database=Test_LYW;uid=sa;pwd=intretech@123";
                    SqlConnection conn = new SqlConnection(sqlstr);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = " insert into data1(data1) values (" + BitConverter.ToString(datashow) + ")";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
                if (datashow[1] == 0x02)
                {
                    string sqlstr = "server=192.168.4.124;database=Test_LYW;uid=sa;pwd=intretech@123";
                    SqlConnection conn = new SqlConnection(sqlstr);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "  insert into newtable1(id,username) values ('5','lyw')";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
                if (datashow[1] == 0x03)
                {
                    string sqlstr = "server=192.168.4.124;database=Test_LYW;uid=sa;pwd=intretech@123";
                    SqlConnection conn = new SqlConnection(sqlstr);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = " insert into data3(data3) values (" + BitConverter.ToString(datashow) + ")";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
                if (datashow[1] == 0x04)
                {
                    string sqlstr = "server=192.168.4.124;database=Test_LYW;uid=sa;pwd=intretech@123";
                    SqlConnection conn = new SqlConnection(sqlstr);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = " insert into data4(data4) values (" + BitConverter.ToString(datashow) + ")";
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };

            }
    }
        private void setText(Byte[] retext)
        {
            string result = " ";
            if (retext[1] == 0x01) {
                Byte[] text = retext;
                switch (text[10]) {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect.Dispatcher.Invoke(new Action(() => { this.datacollect.Text = result; }));
                switch (text[16])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect2.Dispatcher.Invoke(new Action(() => { this.datacollect2.Text = result; }));
                switch (text[18])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect3.Dispatcher.Invoke(new Action(() => { this.datacollect3.Text = result; }));
                switch (text[22])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect4.Dispatcher.Invoke(new Action(() => { this.datacollect4.Text = result; }));
                switch (text[24])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect5.Dispatcher.Invoke(new Action(() => { this.datacollect5.Text = result; }));
                switch (text[26])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect6.Dispatcher.Invoke(new Action(() => { this.datacollect6.Text = result; }));
            }
            if (retext[1] == 0x02) {
                Byte[] text = retext;
                byte[] b = { text[11], text[12] };
                short x = (short)((b[0] << 8) + b[1]);
                this.datacollect7.Dispatcher.Invoke(new Action(() => { this.datacollect7.Text = x.ToString(); }));
                byte[] a = { text[19], text[20] };
                short y = (short)((a[0] << 8) + a[1]);
                byte[] c = { text[27], text[28] };
                short z = (short)((c[0] << 8) + c[1]);
                this.datacollect8.Dispatcher.Invoke(new Action(() => { this.datacollect8.Text = y.ToString(); }));
                this.datacollect9.Dispatcher.Invoke(new Action(() => { this.datacollect9.Text = z.ToString(); }));
            }
            if (retext[1] == 0x03) {
                Byte[] text = retext;
                switch (text[12])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect10.Dispatcher.Invoke(new Action(() => { this.datacollect10.Text = result; }));
                switch (text[16])
                {
                    case 0x00:
                        result = "开";
                        break;
                    case 0x01:
                        result = "关";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect11.Dispatcher.Invoke(new Action(() => { this.datacollect11.Text = result; }));
            }
            if (retext[1] == 0x04)
            {
                Byte[] text = retext;
                switch (text[12])
                {
                    case 0x00:
                        result = "选定值1";
                        break;
                    case 0x01:
                        result = "选定值2";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                this.datacollect12.Dispatcher.Invoke(new Action(() => { this.datacollect12.Text = result; }));
            }
            if (retext[1] == 0x05)
            {
                Byte[] text = retext;
                switch (text[10])
                {
                    case 0x01:
                        result = "选定值1";
                        break;
                    case 0x02:
                        result = "选定值2";
                        break;
                    default:
                        result = "未知数据";
                        break;
                }
                byte[] d = { text[11], text[12] };
                short h = (short)((d[0] << 8) + d[1]);
                this.datacollect14.Dispatcher.Invoke(new Action(() => { this.datacollect14.Text = h.ToString(); }));
                byte[] e = { text[13], text[14] };
                short i = (short)((e[0] << 8) + e[1]);
                this.datacollect17.Dispatcher.Invoke(new Action(() => { this.datacollect17.Text = i.ToString(); }));
                this.datacollect20.Dispatcher.Invoke(new Action(() => { this.datacollect20.Text = result; }));
            }
            if (retext[1] == 0x06)
            {
                Byte[] text = retext;
                this.datacollect22.Dispatcher.Invoke(new Action(() => { this.datacollect22.Text = BitConverter.ToString(text); }));
            }
            if (retext[1] == 0x07)
            {
                Byte[] text = retext;
                byte[] f = { text[9], text[10] };
                byte[] g = { text[11], text[12] };
                short j = (short)((f[0] << 8) + f[1]);
                short k = (short)((g[0] << 8) + g[1]);
                this.Dispatcher.Invoke(new Action(() => { this.datacollect23.Text = ((float)j / k).ToString(); }));
            }
            if (retext[1] == 0x08)
            {
                Byte[] text = retext;
                this.datacollect24.Dispatcher.Invoke(new Action(() => { this.datacollect24.Text = byteToString(text[9], text[10]); }));
                this.datacollect25.Dispatcher.Invoke(new Action(() => { this.datacollect25.Text = byteToString(text[19], text[20]); }));
                this.datacollect26.Dispatcher.Invoke(new Action(() => { this.datacollect26.Text = byteToString(text[23], text[24]); }));
                this.datacollect27.Dispatcher.Invoke(new Action(() => { this.datacollect27.Text = byteToString(text[27], text[28]); }));
                this.datacollect28.Dispatcher.Invoke(new Action(() => { this.datacollect28.Text = byteToString(text[31], text[32]); }));
                this.datacollect29.Dispatcher.Invoke(new Action(() => { this.datacollect29.Text = byteToString(text[35], text[36]); }));
                this.datacollect30.Dispatcher.Invoke(new Action(() => { this.datacollect30.Text = byteToString(text[39], text[40]); }));
                this.datacollect31.Dispatcher.Invoke(new Action(() => { this.datacollect31.Text = byteToString(text[43], text[44]); }));
                this.datacollect32.Dispatcher.Invoke(new Action(() => { this.datacollect32.Text = byteToString(text[51], text[52]); }));
                this.datacollect33.Dispatcher.Invoke(new Action(() => { this.datacollect33.Text = byteToString(text[55], text[56]); }));
                this.datacollect34.Dispatcher.Invoke(new Action(() => { this.datacollect34.Text = byteToString(text[39], text[40]); }));
            }
            if (retext[1] == 0x09)
            {
                Byte[] text = retext;
                this.Dispatcher.Invoke(new Action(() => { this.datacollect39.Text = byteToString(text[9], text[10]); }));
                this.Dispatcher.Invoke(new Action(() => { this.datacollect40.Text = byteToString(text[11], text[12]); }));
                this.datacollect41.Dispatcher.Invoke(new Action(() => { this.datacollect41.Text = byteToString(text[13], text[14]); }));
                this.datacollect42.Dispatcher.Invoke(new Action(() => { this.datacollect42.Text = byteToString(text[15], text[16]); }));
                this.datacollect43.Dispatcher.Invoke(new Action(() => { this.datacollect43.Text = byteToString(text[17], text[18]); }));
                this.datacollect44.Dispatcher.Invoke(new Action(() => { this.datacollect44.Text = byteToString(text[19], text[20]); }));
                this.datacollect45.Dispatcher.Invoke(new Action(() => { this.datacollect45.Text = byteToString(text[21], text[22]); }));
                this.datacollect46.Dispatcher.Invoke(new Action(() => { this.datacollect46.Text = byteToString(text[23], text[24]); }));
            }

            }
        private void sendDataTest(object sender, EventArgs e)
        {
            int error=-11111111;
            //发送数据
            byte[] data = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x10 };
            //地址2550 读取十个寄存器地址
            data = new byte[] { 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x09, 0xf6, 0x00, 0x10 };

            try
            {
                error = newclient.Send(data);
             
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
     
            byte[] data1 = new byte[1024];//定义数据接收数组  
            try
            {
                error=newclient.Receive(data1);//接收数据到data数组  
               
                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
                //   MessageBox.Show(stringdata);
            }
            catch (Exception ex)
            {
                Connect();
            }

            System.Threading.Thread.Sleep(100);

            //发送第二段数据
            //发送数据
            //地址850 读取6个寄存器地址
         
            data = new byte[] { 0x00, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x52, 0x00, 0x06 };

            try
            {
            error=  newclient.Send(data);
               
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
              error =  newclient.Receive(data1);//接收数据到data数组  
              
                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                // MessageBox.Show(stringdata);
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);
            //第三部分数据
            //地址880 读取六个寄存器地址


            try
            {
                data = new byte[] { 0x00, 0x06, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x70, 0x00, 0x06 };
                error= newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
                error=newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
                //   MessageBox.Show(stringdata);
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);

            //发送第四段数据
            //发送数据
            //地址800 读取2个寄存器地址 因数为2个寄存器数据比值


            try
            {
                data = new byte[] { 0x00, 0x07, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0x20, 0x00, 0x02 };
              error=  newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
                error=newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                // MessageBox.Show(stringdata);
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);
            //读取第五段数据
            //地址1000 读取二十个寄存器地址
            data = new byte[] { 0x00, 0x08, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x03, 0xe8, 0x00, 0x20 };

            try
            {
               error= newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据

            try
            {
               error= newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
                //   MessageBox.Show(stringdata);
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);

            //发送第六段数据
            //发送数据
            //冷干机数据地址40001 读取6个寄存器地址
            data = new byte[] { 0x00, 0x09, 0x00, 0x00, 0x00, 0x06, 0x32, 0x03, 0x9C, 0x41, 0x00, 0x08 };

            try
            {
                error=newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
               error= newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                // MessageBox.Show(stringdata);
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);
            //第七部分数据
            //地址0 读取十个寄存器地址
            data = new byte[] { 0x00, 0x02, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x00, 0x00, 0x10 };

            try
            {
                error=newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
               error= newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
                //   MessageBox.Show(stringdata);
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);

            //发送第八段数据
            //发送数据
            //地址500 读取16个寄存器地址 
            data = new byte[] { 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x01, 0xf4, 0x00, 0x16 };

            try
            {
               error= newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();

            }
            //接收数据
            try
            {
               error= newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                // MessageBox.Show(stringdata);
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
            }
            catch (Exception ex)
            {
                Connect();
            }
            System.Threading.Thread.Sleep(100);
            //发送第九段数据
            //发送数据
            //地址518 读取2个寄存器地址 
            data = new byte[] { 0x00, 0x04, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x02, 0x06, 0x00, 0x02 };

            try
            {
                error=newclient.Send(data);
            }
            catch (Exception ex)
            {
                Connect();
            }
            //接收数据
            try
            {
               error= newclient.Receive(data1);//接收数据到data数组  

                int length1 = data1[5];//读取数据长度  
                Byte[] datashow1 = new byte[length1 + 6];//定义所要显示的接收的数据的长度  
                for (int i = 0; i <= length1 + 5; i++)//将要显示的数据存放到数组datashow中  
                    datashow1[i] = data1[i];
                // MessageBox.Show(stringdata);
                SetTextCallback1 d1 = new SetTextCallback1(setText);   // 托管调用  
                this.Dispatcher.Invoke(d1, new object[] { datashow1 });
            }
            catch (Exception ex)
            {
                Connect();
            }
        }


    }
}
